﻿Public Class vCardItem

	Private Const UnicodeValue As String = "CHARSET=utf-8"
	Private Const QuotedPrintableValue As String = "ENCODING=QUOTED-PRINTABLE"

	Protected Overridable ReadOnly Property IsMultiline As Boolean
		Get
			If vParameters.Count = 1 AndAlso Not String.IsNullOrEmpty(vParameters(0)) AndAlso vParameters(0).Contains(Environment.NewLine) Then
				Return True
			Else
				Return False
			End If
		End Get
	End Property

	Protected Overridable ReadOnly Property IsUnicodeString As String
		Get
			If vParameters.Any(Function(x) x.IsUnicode) Then
				Return True
			Else
				Return False
			End If
		End Get
	End Property

	Protected Overridable Property vProperty As String
	Protected Overridable Property vValues As List(Of String)
	Protected Overridable Property vParameters As List(Of String)

	Public Sub New(vProperty As String, vValues As List(Of String), vParameters As List(Of String))
		NewBuilder(vProperty, vValues, vParameters)
	End Sub

	Public Sub New(vProperty As String, vValue As String, vParameter As String)
		NewBuilder(vProperty, New String() {vValue}.ToList, New String() {vParameter}.ToList)
	End Sub

	Public Sub New(vProperty As String, vValues As List(Of String), vParameter As String)
		NewBuilder(vProperty, vValues, New String() {vParameter}.ToList)
	End Sub

	Public Sub New(vProperty As String, vValue As String, vParameters As List(Of String))
		NewBuilder(vProperty, New String() {vValue}.ToList, vParameters)
	End Sub

	Public Sub New(vProperty As String, vParameter As String)
		NewBuilder(vProperty, Nothing, New String() {vParameter}.ToList)
	End Sub

	Public Sub New(vProperty As String, vParameters As List(Of String))
		NewBuilder(vProperty, Nothing, vParameters)
	End Sub

	Public Sub New(vProperty As String)
		NewBuilder(vProperty, Nothing, Nothing)
	End Sub

	Public Sub New()

	End Sub

	Private Sub NewBuilder(vProperty As String, vValues As List(Of String), vParameters As List(Of String))
		Me.vProperty = vProperty
		Me.vValues = vValues
		Me.vParameters = vParameters
	End Sub

	Public Overridable Shadows Function ToString() As String
		If Not String.IsNullOrEmpty(vProperty) Then
			Dim myValues As List(Of String) = If(vValues Is Nothing, New List(Of String), vValues.ToList)
			Dim myParameters As List(Of String) = If(vParameters Is Nothing, New List(Of String), vParameters.ToList)

			If myParameters.Count > 1 Then
				myParameters = myParameters.Select(Function(x) x.EscapeParameters).AsParallel.ToList
			End If
			If IsUnicodeString Then
				myValues.Add(UnicodeValue)
			End If
			If IsMultiline Then
				myValues.Add(QuotedPrintableValue)
				myParameters = myParameters.Select(Function(x) x.ToQuotedPrintable).AsParallel.ToList
			End If

			Dim myPropertyValue As New List(Of String)
			myPropertyValue.Add(vProperty)
			If myValues.Count > 0 Then
				myPropertyValue.AddRange(myValues)
			End If

			Return String.Format("{0}:{1}", String.Join(";", myPropertyValue), String.Join(";", myParameters))
		Else
			Return ""
		End If
	End Function

End Class
